#ifndef LIBTORRENT_BITFIELD_H
#define LIBTORRENT_BITFIELD_H
#include <cstddef>
#include <cstring>

namespace torrent {

class BitField {
public:
	BitField() : m_size(0), m_start(NULL), m_end(NULL) {}

	BitField(unsigned int s) : m_size(s) {
		m_start = s ? new int[sizeInts()] : NULL;
		m_end = (char*)m_start + (s + 7) / 8;

		clear();
	}

	~BitField();

	unsigned int sizeBits() const { return m_size; }
	unsigned int sizeBytes() const { return m_end - (char*)m_start; }
	unsigned int sizeInts() const { return (m_size + 8 * sizeof(int) - 1) / (8 * sizeof(int)); }

	void clear() {
		if (m_start) memset(m_start, 0, sizeInts() * sizeof(int));
	}

	bool zero() const;

	char* data() { return (char*)m_start; }
	const char* data() const { return (char*)m_start; }

private:
	int m_size;
	int* m_start;
	char* m_end;
};

} // namespace torrent

#endif // LIBTORRENT_BITFIELD_H
